package com.my.controller;

import com.my.entity.*;
import com.my.service.StudentService;
import jakarta.servlet.ServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/students")
    public Result select(StudentQueryParam param){
        log.info("请求参数:{}",param);

        // 返回 总记录数和 当前数据列表
        PageBean pageBean = studentService.select(param);

        return Result.success(pageBean);
    }

    /**
     * 新增学员
     * @param student
     * @return
     */
    @PostMapping("/students")
    public Result insert(@RequestBody Student student){
        log.info("新增的学员:{}",student);

        studentService.insert(student);
        return Result.success();
    }

    /**
     * 通过Id回显数据
     * @param id
     * @return
     */
    @GetMapping("/students/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("学生ID:{}",id);
        // 调用service
        Student student = studentService.selectById(id);

        return Result.success(student);
    }


    /**
     * 删除学员
     * @param ids
     * @return
     */
    @DeleteMapping("/students/{ids}")
    public Result delete(@RequestParam Integer[] ids){
        log.info("删除的学员ids:{}",ids.toString());
        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 修改学员信息
     */
    @PutMapping("/students")
    public Result update(@RequestBody Student student){
        log.info("传入值:{}",student);

        // 调用service
        studentService.update(student);
        return Result.success();
    }

    /**
     * 违纪处理
     */
    @PutMapping("/students/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score, ServletRequest servletRequest){
        log.info("id:{},score:{}",id,score);

        studentService.violation(id,score);

        return Result.success();
    }


    /**
     * 学历统计
     */
    @GetMapping("/report/studentDegreeData")
    public Result getStudentDegreeData(){

        List<DegreeCount> list = studentService.getStudentDegreeData();
        return Result.success(list);
    }
}
